#include "main.h"
#include "Global.h"
#include "Functions.h"
#include "CPlayer.h"
#include "CAssault.h"
#include "CWorld.h"
#include "Foreach.h"
#include "Collision.h"

//
// TEMPORARY FOR DEBUG PURPOSES
//
CPlayer* g_pPlayer = NULL;

CPlayer::CPlayer(ENTITY_PARAMS* params){
	Init(params);
	m_prevpos=m_pos;
	m_nextpos=m_pos;
	m_friction = V(0.03f,0.03f);
	m_animationa=0;
	m_animationb=0;
	m_frame=0;
	m_bJumped=false;
	m_pWeapon=NULL;
	m_pAssault = new CAssault(true, this);
	m_bRUNONCE=true;

	m_lastCollisionPos = V0;

	m_pCollider->SetCollisionType(Dynamic);
};

CPlayer::~CPlayer(){
	
};

void CPlayer::Step(CEntityList *_EntList, BYTE _keys[256], DIMOUSESTATE mouse_state){
	if (m_bRUNONCE){
		m_bRUNONCE=false;
		_EntList->add(m_pAssault);
	}
	//Input

	if (_keys[DIK_A])
		m_accel.x = -0.2f;
	else if (_keys[DIK_D])
		m_accel.x = 0.2f;
	else
		m_accel.x=0;
	
	if (_keys[DIK_1]){
		m_pWeapon = m_pAssault;
	}

	if (_keys[DIK_W] && !m_bJumped){
		m_bJumped=true;
		m_accel.y = -10.0f;
	}else{
		m_accel.y = 0;
	}
	//else if (_keys[DIK_S])
	//	m_accel.y = 0.4f;
	

	if(mouse_state.rgbButtons[0] && m_pWeapon == m_pAssault){
		m_frame=1;
		_EntList->add(m_pWeapon->Shoot());
	}
	else
		m_frame=0;

    if(m_animationa>=360)
		m_animationa=0;
	    if(m_animationa<=-360)
		m_animationa=0;

	m_animationa+=m_accel.x/2;
	m_animationb+=0.05f;


	//Movement
	// VERLET
	Verlet();
	
	m_angle=-(m_pos.x-m_prevpos.x)*6;
	//m_pos.x = min(max(m_pos.x, m_halfwidth), LEVEL_WIDTH-m_halfwidth);
	//m_pos.y = min(max(m_pos.y, m_halfheight), LEVEL_HEIGHT-m_halfheight);
	CAMERA.x=m_pos.x-WINDOW_CENTER.x;
	CAMERA.y=m_pos.y-WINDOW_CENTER.y-200;
	//if(m_pos.x-CAMERA.x>WINDOW_CENTER.x || m_pos.x-CAMERA.x<WINDOW_CENTER.y)
		//CAMERA.x+=WINDOW_CENTER.x+CAMERA.x-m_pos.x;
//	if(m_pos.y-CAMERA.y>600 || m_pos.y-CAMERA.y<150)
//		CAMERA.y+=WINDOW_CENTER.y+CAMERA.y-m_pos.y;
	CAMERA.x = floor(min(max(CAMERA.x, 0), LEVEL_WIDTH-WINDOW_WIDTH));
	CAMERA.y = floor(min(max(CAMERA.y, 0), LEVEL_HEIGHT-WINDOW_HEIGHT));

	if(MOUSE.x<m_pos.x)
		m_dir=-1;
	else
		m_dir=1;
	
};

void CPlayer::onCollision(const Vector2& collisionResult, CEntity* pCollideEnt)
{
	if( collisionResult.y < 0 )
		m_bJumped=false;

}

void CPlayer::Draw(Sprites* _Sprite){	
	DrawSprite(
		_Sprite->Char->ME->Feet,
		m_frame,
		V(m_pos.x-sin(m_animationa)*8,m_pos.y+6-(m_pos.y-m_prevpos.y)),
		V(m_scale.x*m_dir, m_scale.y),
		((m_pos.y-m_prevpos.y)*6)*m_dir,
		CLR_WHITE);

	DrawSprite(
		_Sprite->Char->ME->Feet,
		m_frame,
		V(m_pos.x+sin(m_animationa)*8+(3*m_dir),m_pos.y+6-(m_pos.y-m_prevpos.y)),
		V(m_scale.x*m_dir, m_scale.y),
		((m_pos.y-m_prevpos.y)*6)*m_dir,
		CLR_WHITE);
	
	DrawSprite(
		_Sprite->Char->ME->Torso,
		m_frame,
		V(m_pos.x,m_pos.y-18-((m_pos.y-m_prevpos.y)/1.5f)-sin(m_animationb)*3), 
		V(m_scale.x*m_dir, m_scale.y),
		m_angle/2,
		CLR_WHITE);

	DrawSprite(
		_Sprite->Char->ME->FullHead,
		m_frame,
		V(m_pos.x+(((m_pos.x-m_prevpos.x)/1.2f)),m_pos.y-39-sin(m_animationb)*2),
		V(m_scale.x*m_dir, m_scale.y),
		D3DXToDegree(atan2(MOUSE.x-m_pos.x, MOUSE.y-m_pos.y))-(90*m_dir),
		CLR_WHITE);

	DrawRect( V(m_pos.x,m_pos.y), getWidth(), getHeight(), D3DCOLOR_RGBA(255,0,0,0), 1, D3DCOLOR_RGBA(255,0,0,255));


	if (m_pWeapon && m_pWeapon->m_pSprite){\
		m_pWeapon->setPos(m_pos);
	//	m_pWeapon->Draw(_Sprite);
	}

	//g_pDrawLine(m_pos, V(0, 0), D3DCOLOR_ARGB(255, 255, 0, 0));
	//DrawText(g_pFontList->Arial20B, "<-Pro Line", m_pos.x, m_pos.y-32, D3DCOLOR_ARGB(255, 0, 0, 255));
	//for(int i=1; i<=4; i++)
		//for(int j=1; j<=4;j++)
	
	DrawTextExact(g_pFontList->Arial20,toStr(ENTITIES),V(16,10),CLR_BLACK);
			//DrawTileset(_Sprite->Level->TSBuilding1, j, i, 200+(32*(float)j), 500+(32*(float)i), 1, 1, 0, D3DXVECTOR3(255, 255, 255), 255);

	/*if( m_lastCollisionPos.x || m_lastCollisionPos.y )
		DrawLine(m_pos,m_lastCollisionPos,D3DCOLOR_RGBA(255,0,0,255));*/

/*	FOREACHS(CCollisionManager::Cell*,m_pCollider->m_pCells,i)
	{
		if( !*i )
			continue;

		FOREACHS(CEntity*,(*i)->m_entList, e)
		{
			DrawLine(m_pos,(*e)->getPos(),D3DCOLOR_RGBA(255,0,0,255));
		}
	}*/
};
